﻿<UserControl x:Name="_root"
	x:Class="Shortcuts.ShortcutsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:con="http://schemas.kent.boogaart.com/converters"
	xmlns:sys="clr-namespace:System;assembly=mscorlib"
	xmlns:local="clr-namespace:Shortcuts">
	<UserControl.Resources>
		<ContextMenu x:Key="ContextMenu"/>

		<con:MapConverter x:Key="NoItemsVisibilityConverter">
			<con:Mapping To="{x:Static Visibility.Visible}">
				<con:Mapping.From>
					<sys:Int32>0</sys:Int32>
				</con:Mapping.From>
			</con:Mapping>
		</con:MapConverter>	
	</UserControl.Resources>

	<DockPanel DataContext="{Binding ElementName=_root}" ContextMenu="{StaticResource ContextMenu}" Background="#00ffffff" KeyboardNavigation.TabNavigation="Cycle">
		<StackPanel>
			<Label Visibility="{Binding Presenter.ShortcutGroups.Count, Converter={StaticResource NoItemsVisibilityConverter}, FallbackValue={x:Static Visibility.Collapsed}}">
				<TextBlock>
					No shortcut groups have been added. Click
					<Hyperlink Command="{Binding Presenter.AddShortcutGroupCommand}">here</Hyperlink>
					or right-click to add a shortcut.
				</TextBlock>
			</Label>
			<ItemsControl x:Name="_itemsControl" ItemsSource="{Binding Presenter.ShortcutGroups}" MouseRightButtonDown="_itemsControl_MouseRightButtonUp">
				<ItemsControl.CommandBindings>
					<CommandBinding Command="{x:Static local:Commands.RenameShortcutGroup}" CanExecute="_shortcutGroupCommand_CanExecute" Executed="_renameShortcutGroup_Executed"/>
					<CommandBinding Command="{x:Static local:Commands.DeleteShortcutGroup}" CanExecute="_shortcutGroupCommand_CanExecute" Executed="_deleteShortcutGroup_Executed"/>
					<CommandBinding Command="{x:Static local:Commands.AddShortcut}" CanExecute="_shortcutGroupCommand_CanExecute" Executed="_addShortcut_Executed"/>
					<CommandBinding Command="{x:Static local:Commands.DeleteShortcut}" CanExecute="_shortcutCommand_CanExecute" Executed="_deleteShortcut_Executed"/>
				</ItemsControl.CommandBindings>
				
				<ItemsControl.ItemTemplate>
					<DataTemplate>
						<Expander IsExpanded="{Binding IsExpanded}">
							<Expander.CommandBindings>
								<CommandBinding Command="{x:Static local:Commands.RenameShortcutGroup}" CanExecute="_shortcutGroupCommand_CanExecute" Executed="_renameShortcutGroup_Executed"/>
								<CommandBinding Command="{x:Static local:Commands.DeleteShortcutGroup}" CanExecute="_shortcutGroupCommand_CanExecute" Executed="_deleteShortcutGroup_Executed"/>
								<CommandBinding Command="{x:Static local:Commands.AddShortcut}" CanExecute="_shortcutGroupCommand_CanExecute" Executed="_addShortcut_Executed"/>
								<CommandBinding Command="{x:Static local:Commands.DeleteShortcut}" CanExecute="_shortcutCommand_CanExecute" Executed="_deleteShortcut_Executed"/>
							</Expander.CommandBindings>
							
							<Expander.Header>
								<StackPanel>
									<TextBlock Text="{Binding Name}">
										<TextBlock.Visibility>
											<MultiBinding Converter="{StaticResource UnderEditConverter}" ConverterParameter="TextBlock">
												<Binding/>
												<Binding Path="GroupUnderEdit" ElementName="_root"/>
											</MultiBinding>
										</TextBlock.Visibility>
									</TextBlock>
									<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" PreviewLostKeyboardFocus="_groupNameTextBox_PreviewLostKeyboardFocus" IsVisibleChanged="_textBox_IsVisibleChanged" PreviewTextInput="_groupNameTextBox_PreviewTextInput">
										<TextBox.Visibility>
											<MultiBinding Converter="{StaticResource UnderEditConverter}" ConverterParameter="TextBox">
												<Binding/>
												<Binding Path="GroupUnderEdit" ElementName="_root"/>
											</MultiBinding>
										</TextBox.Visibility>
									</TextBox>
								</StackPanel>
							</Expander.Header>
							<StackPanel>
								<Label Visibility="{Binding Shortcuts.Count, Converter={StaticResource NoItemsVisibilityConverter}, FallbackValue={x:Static Visibility.Collapsed}}">
									<TextBlock>
										No shortcuts have been added to this group. Click
										<Hyperlink Command="{x:Static local:Commands.AddShortcut}" CommandParameter="{Binding}">here</Hyperlink>
										or right-click to add a shortcut.
									</TextBlock>
								</Label>
								<ItemsControl ItemsSource="{Binding Shortcuts}" ItemTemplate="{DynamicResource ShortcutTemplate}"/>
							</StackPanel>
						</Expander>
					</DataTemplate>
				</ItemsControl.ItemTemplate>
			</ItemsControl>
		</StackPanel>
	</DockPanel>
</UserControl>
